package com.framework.loippi.utils;

import javax.servlet.http.HttpServletRequest;

import com.framework.loippi.utils.http.HttpUtils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 百度公共工具包
 * <p>
 * 1. 获取天气
 */
public class BaiduApiUtils {

    public static final String DEFAULT_CITY_NAME = "广州";


    private static final String ak = "2m9D9S9kygG7pfYwZGsZ1Mfx";

    /**
     * 获取天气
     */
    public static String weather(String cityName) {
        String result = HttpUtils.create().addHeader("apikey", "519d6585b31c1055fb8b3d677e820a12").
                addParam("cityname", cityName).get("http://apis.baidu.com/apistore/weatherservice/cityname");
        if (JSONObject.fromObject(result).getInt("errNum") != 0) {
            return null;
        }
        JSONObject jSONObject = JSONObject.fromObject(result);
        jSONObject = JSONObject.fromObject(jSONObject.get("retData"));
        String weather = jSONObject.get("weather").toString();
        return weather;
    }

    public static int getIdx(String weather) {
        if (weather.contains("晴")) {
            return 1;
        }
        if (weather.contains("云")) {
            return 2;
        }
        if (weather.contains("阴")) {
            return 3;
        }
        if (weather.contains("雨")) {
            return 4;
        }
        if (weather.contains("雾")) {
            return 5;
        }
        if (weather.contains("霾")) {
            return 6;
        }
        if (weather.contains("雪")) {
            return 7;
        }
        return 0;
    }


    /**
     * 获取天气  百度 车联网 API
     */
    public static JSONObject weather2(String cityName) {
        String result = HttpUtils.create().addParam("ak", ak).
                addParam("output", "json").addParam("location", cityName).get("http://api.map.baidu.com/telematics/v3/weather");
        System.err.println(result);

        return JSONObject.fromObject(result);
    }


    /**
     * 根据IP地址获取城市
     */
    public static String getCityByIP(String ip) {
        try {
            String result = HttpUtils.create().addParam("ak", ak).
                    addParam("ip", ip).addParam("coor", "bd09ll").get("http://api.map.baidu.com/location/ip");
            System.err.println(result);

            JSONObject jSONObject = JSONObject.fromObject(result);
            if (jSONObject.getInt("status") != 0) {
                return null;
            }
            jSONObject = JSONObject.fromObject(jSONObject.get("content"));
            jSONObject = JSONObject.fromObject(jSONObject.get("address_detail"));
            return jSONObject.get("city").toString();
        } catch (Exception e) {
            return null;
        }
    }


    /**
     * 根据IP地址获取城市
     */
    public static String getProvinceByIP(String ip) {
        try {
            String result = HttpUtils.create().addParam("ak", ak).
                    addParam("ip", ip).addParam("coor", "bd09ll").get("http://api.map.baidu.com/location/ip");
            JSONObject jSONObject = JSONObject.fromObject(result);
            if (jSONObject.getInt("status") != 0) {
                return null;
            }
            jSONObject = JSONObject.fromObject(jSONObject.get("content"));
            jSONObject = JSONObject.fromObject(jSONObject.get("address_detail"));
            return jSONObject.get("province").toString();
        } catch (Exception e) {
            return null;
        }

    }


    public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

    public static void main(String[] args) {
        String ff = weather("广州");
        System.out.println(ff);
    }

}
